#!/bin/bash

# This bash script only works on LoongArch64 buildbot Yukoaioi, since I specify the local path where I built debs.
# After running this stage1 script, chroot into TARGETDIR then use APT locally to install other essential packages. (TODO)

# current temp stage2

# first run a simple flat local APT repository, like
# python3 -m http.server -d /clfs-test-bak/clfs-test/debs
# edit $TARGETDIR/etc/apt/sources.list to add the following line
# deb [allow-insecure=yes] http://0.0.0.0:8000 ./

export TARGETDIR=/mnt/aosc
mkdir -pv /tmp/work

cd /tmp/work
echo "------ar aosc-aaa_8.1.0-0_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/a/aosc-aaa_8.1.0-0_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
wget https://repo.aosc.io/debs/pool/stable/main/c/ca-certs_20201201-1_noarch.deb
echo "------ar ca-certs_20201201-1_noarch.deb to /tmp/work------"
ar -x ca-certs_20201201-1_noarch.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar tar_1.32-1_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/t/tar_1.32-1_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar diffutils_3.7-5_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/d/diffutils_3.7-5_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar coreutils_8.31-3_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/c/coreutils_8.31-3_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar apt_2.3.8-0_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/a/apt_2.3.8-0_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar dpkg_1.20.9-1_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/d/dpkg_1.20.9-1_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar xz_5.2.5-1_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/x/xz_5.2.5-1_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar zlib_1.2.11-4_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/z/zlib_1.2.11-4_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar zstd_1.5.0-0_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/z/zstd_1.5.0-0_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar bzip2_1.0.8-2_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/b/bzip2_1.0.8-2_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar lz4_1.9.2-1_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/l/lz4_1.9.2-1_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar gnupg_2.2.23-1_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/g/gnupg_2.2.23-1_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar bash_5.1.8-0_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/b/bash_5.1.8-0_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar glibc_2.28-0_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/g/glibc_2.28-0_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar readline_8.1.1-0_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/r/readline_8.1.1-0_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar ncurses_6.2-4_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/n/ncurses_6.2-4_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar gcc-runtime_8.3.0-0_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/g/gcc-runtime_8.3.0-0_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar systemd_248.3-1_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/s/systemd_248.3-1_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar libgcrypt_1.9.1-0_loongarch64.deb  to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/libg/libgcrypt_1.9.1-0_loongarch64.deb 
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar libgpg-error_1.41-0_loongarch64.deb  to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/libg/libgpg-error_1.41-0_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar xxhash_0.8.0-0_loongarch64.deb  to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/x/xxhash_0.8.0-0_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar libcap_2.43-2_loongarch64.deb  to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/libc/libcap_2.43-2_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar libseccomp_2.5.1-1_loongarch64.deb  to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/libs/libseccomp_2.5.1-1_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar gnutls_3.6.15-1_loongarch64.deb  to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/g/gnutls_3.6.15-1_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar p11-kit_0.23.18.1-1_loongarch64.deb  to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/p/p11-kit_0.23.18.1-1_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar libunistring_0.9.10-3_loongarch64.deb  to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/libu/libunistring_0.9.10-3_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar libunistring_0.9.10-3_loongarch64.deb  to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/libu/libunistring_0.9.10-3_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar libtasn1_4.15.0-2_loongarch64.deb  to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/libt/libtasn1_4.15.0-2_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar nettle_3.7.2-0_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/n/nettle_3.7.2-0_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar gmp_6.2.1-0_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/g/gmp_6.2.1-0_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar libffi_3.2.1-11_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/libf/libffi_3.2.1-11_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar acl_2.2.53-2_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/a/acl_2.2.53-2_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

cd /tmp/work
echo "------ar attr_2.4.48-2_loongarch64.deb to /tmp/work------"
ar -x /clfs-test-bak/clfs-test/debs/a/attr_2.4.48-2_loongarch64.deb
cd $TARGETDIR
echo "------extracting data.tar.xz to $TARGETDIR------"
tar xvf /tmp/work/data.tar.xz -C $TARGETDIR

echo "------wget etc-bootstrap.tar.xz to $TARGETDIR------"
wget https://repo.aosc.io/aosc-repacks/etc-bootstrap.tar.xz
echo "------extracting etc-bootstrap.tar.xz to $TARGETDIR------"
tar xvpf etc-bootstrap.tar.xz -C $TARGETDIR
rm etc-bootstrap.tar.xz

echo "------mkdir for $TARGETDIR/var/lib/dpkg, and create empty dpkg/{status, available}------"
mkdir -p "$TARGETDIR/var/lib/dpkg"
: >"$TARGETDIR/var/lib/dpkg/status"
: >"$TARGETDIR/var/lib/dpkg/available"
